/* Oscar Medina
 * http://sharepointace.com/blog
 * http://www.codeplex.com/CQWPRCE
 * 
 * This is an enhanced version of the ContentByQueryWebPart
 * It adds support for showing recurring calendar events which do not
 * show on the OOTB version.
 */
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Xml.Serialization;
using System.ComponentModel;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Threading;
using Microsoft.Office.Server.Diagnostics;
using System.Web;
using System.Runtime.InteropServices;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebControls;

namespace SharePointAce.SharePoint.WebParts
{
    public class SharePointAceContentByQueryWebPartToolPart : ToolPart
    {
        #region Private

        private SharePointAceCQWPRCE contentQueryWebpart;
        private CheckBox chkBox;
        private CheckBox chkBoxShowRecurringCalendarEvents;
        private HiddenField retQry;
        private TextBox txtViewFields;
        private TextBox txtMainXslLink;
        private TextBox txtItemXslLink;

        #endregion

        #region protected override void OnInit(EventArgs e)

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Title = "SharePointAce ToolPart";
            this.contentQueryWebpart = base.ParentToolPane.SelectedWebPart as SharePointAceCQWPRCE;
        }

        #endregion

        #region protected override void CreateChildControls()

        protected override void CreateChildControls()
        {
            TableCell cell;
            Table table;
            TableRow row;
            Label label;

            table = new Table();
            table.Width = Unit.Percentage(100);

            #region Query Fields Override

            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.ControlStyle.Font.Bold = true;
            label.Text = "Query Override";
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.Text = "To Add or Edit,Click Editor";
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();

            retQry = new HiddenField();
            retQry.ID = "Query_HIDDEN";
            cell.Controls.Add(retQry);

            string builderUrl = "/_layouts/zoombldr.aspx";
            string editorId = this.ClientID + '_' + retQry.ID;
            string dialogFeatures = "dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes";

            cell.Controls.Add(new LiteralControl("&nbsp;<input type=\"button\" value=\"Editor...\" id=\"" + editorId + "_QueryOverride_Builder\" title=\" Click to Use Builder \" onclick=\"javascript:" +
            string.Format("MSOPGrid_doBuilder('{0}?culture={1}', {2}, '{3}')", SPHttpUtility.EcmaScriptStringLiteralEncode(builderUrl), Thread.CurrentThread.CurrentUICulture, SPHttpUtility.EcmaScriptStringLiteralEncode(editorId), dialogFeatures) + ";\" />"));

            row.Cells.Add(cell);
            table.Rows.Add(row);

            #endregion

            #region Pick Values from QueryString

            row = new TableRow();
            cell = new TableCell();
            cell.ColumnSpan = 2;
            chkBox = new CheckBox();
            chkBox.Text = "Pick Values from QueryString";
            cell.Controls.Add(chkBox);

            row.Cells.Add(cell);
            table.Rows.Add(row);

            #endregion

            #region Show calendar recurring events
            row = new TableRow();
            cell = new TableCell();
            HtmlGenericControl br = new HtmlGenericControl("hr");
            br.Style.Add("Color", "#cccccc");
            cell.Controls.Add(br);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.ControlStyle.Font.Bold = true;
            label.Text = "Calendar Rollup";
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.Text = "To display recurring events for Calendar List Types, check the box.";
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            chkBoxShowRecurringCalendarEvents = new CheckBox();
            chkBoxShowRecurringCalendarEvents.Text = "Show Calendar recurring Events..";
            cell.Controls.Add(chkBoxShowRecurringCalendarEvents);

            row.Cells.Add(cell);
            table.Rows.Add(row);

            #endregion

            #region HR

            row = new TableRow();
            cell = new TableCell();
            cell.Controls.Add(new System.Web.UI.HtmlControls.HtmlGenericControl("hr"));
            row.Cells.Add(cell);
            table.Rows.Add(row);

            #endregion

            #region Common View Fields Override

            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.ControlStyle.Font.Bold = true;
            label.Text = "CommonViewFields";
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();

            txtViewFields = new TextBox();
            txtViewFields.ID = "txt_viewFields";
            cell.Controls.Add(txtViewFields);

            builderUrl = "/_layouts/zoombldr.aspx";
            editorId = this.ClientID + '_' + txtViewFields.ID;
            dialogFeatures = "dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes";

            cell.Controls.Add(new LiteralControl("&nbsp;<input type=\"button\" value=\"...\" id=\"" + editorId + "_QueryOverride_Builder\" title=\" Click to Use Builder \" onclick=\"javascript:" +
            string.Format("MSOPGrid_doBuilder('{0}?culture={1}', {2}, '{3}')", SPHttpUtility.EcmaScriptStringLiteralEncode(builderUrl), Thread.CurrentThread.CurrentUICulture, SPHttpUtility.EcmaScriptStringLiteralEncode(editorId), dialogFeatures) + ";\" />"));

            row.Cells.Add(cell);
            table.Rows.Add(row);

            #endregion

            #region ContentQueryMain Override

            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.ControlStyle.Font.Bold = true;
            label.Text = "Content Query Main Xsl";
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();

            txtMainXslLink = new TextBox();
            txtMainXslLink.ID = "txt_mainXslLink";
            cell.Controls.Add(txtMainXslLink);

            builderUrl = "/_layouts/zoombldr.aspx";
            editorId = this.ClientID + '_' + txtMainXslLink.ID;
            dialogFeatures = "dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes";

            cell.Controls.Add(new LiteralControl("&nbsp;<input type=\"button\" value=\"...\" id=\"" + editorId + "_QueryOverride_Builder\" title=\" Click to Use Builder \" onclick=\"javascript:" +
            string.Format("MSOPGrid_doBuilder('{0}?culture={1}', {2}, '{3}')", SPHttpUtility.EcmaScriptStringLiteralEncode(builderUrl), Thread.CurrentThread.CurrentUICulture, SPHttpUtility.EcmaScriptStringLiteralEncode(editorId), dialogFeatures) + ";\" />"));

            row.Cells.Add(cell);
            table.Rows.Add(row);

            #endregion

            #region Item Style Xsl Link Override

            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.ControlStyle.Font.Bold = true;
            label.Text = "Item Style Xsl";
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();

            txtItemXslLink = new TextBox();
            txtItemXslLink.ID = "txt_itemXslLink";
            cell.Controls.Add(txtItemXslLink);

            builderUrl = "/_layouts/zoombldr.aspx";
            editorId = this.ClientID + '_' + txtItemXslLink.ID;
            dialogFeatures = "dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes";

            cell.Controls.Add(new LiteralControl("&nbsp;<input type=\"button\" value=\"...\" id=\"" + editorId + "_QueryOverride_Builder\" title=\" Click to Use Builder \" onclick=\"javascript:" +
            string.Format("MSOPGrid_doBuilder('{0}?culture={1}', {2}, '{3}')", SPHttpUtility.EcmaScriptStringLiteralEncode(builderUrl), Thread.CurrentThread.CurrentUICulture, SPHttpUtility.EcmaScriptStringLiteralEncode(editorId), dialogFeatures) + ";\" />"));

            row.Cells.Add(cell);
            table.Rows.Add(row);

            #endregion

            this.Controls.Add(table);

            if (!string.IsNullOrEmpty(this.contentQueryWebpart.customQueryOverride))
                retQry.Value = this.contentQueryWebpart.customQueryOverride;

            if (!string.IsNullOrEmpty(this.contentQueryWebpart.customViewFieldsOverride))
                txtViewFields.Text = this.contentQueryWebpart.customViewFieldsOverride;

            if (!string.IsNullOrEmpty(this.contentQueryWebpart.MainXslLinkOverride))
                txtMainXslLink.Text = this.contentQueryWebpart.MainXslLinkOverride;

            if (!string.IsNullOrEmpty(this.contentQueryWebpart.ItemXslLinkOverride))
                txtItemXslLink.Text = this.contentQueryWebpart.ItemXslLinkOverride;

            chkBox.Checked = this.contentQueryWebpart.useQueryString;
            chkBoxShowRecurringCalendarEvents.Checked = this.contentQueryWebpart.showRecurringCalendarEvents;
        }

        #endregion

        #region public override void ApplyChanges()

        public override void ApplyChanges()
        {
            base.ApplyChanges();
            this.contentQueryWebpart.customQueryOverride = retQry.Value;
            this.contentQueryWebpart.useQueryString = chkBox.Checked;
            this.contentQueryWebpart.showRecurringCalendarEvents = chkBoxShowRecurringCalendarEvents.Checked;
            this.contentQueryWebpart.customViewFieldsOverride = txtViewFields.Text;
            this.contentQueryWebpart.MainXslLinkOverride = txtMainXslLink.Text;
            this.contentQueryWebpart.ItemXslLinkOverride = txtItemXslLink.Text;
        }

        #endregion
    }
}
